﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
using System.Linq;
using Intex.Tools;
namespace WPFExcelReport
{
    public class SizeColorMatrix
    {
        public string Line { get; set; }
        public string Color { get; set; }
        public string Size { get; set; }
        public int Qty { get; set; }
    }

    public class YCAFS05ADDData : BaseFunction
    {
        public INTEXDBC INTEXDBC;
        public SETINTEXDB INTEXDB = new SETINTEXDB();
        public DataSet SetCAD;
        public DataSet SetExt
        {
            get
            {
                return Convert2Intex.SetExt;
            }
            set
            {
                Convert2Intex.SetExt = value;
            }
        }
        public Convert2Intex Convert2Intex;
        public YCAFS05ADDData()
            : base()
        {

        }
        public YCAFS05ADDData(OracleConnection _OracleDB, IntexReptServBLClient _proxy, string userID = "        ")
            : base(_OracleDB, _proxy, userID)
        {
            INTEXDBC = new INTEXDBC(_OracleDB, _proxy);
            Convert2Intex = new Convert2Intex();
            Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);
        }
        public YCAFS05ADDData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
            INTEXDBC = new INTEXDBC(OracleDB, proxy);
            Convert2Intex = new Convert2Intex();
            Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);
        }
        public DataSet GetSData(DataRow dwInput, string mtype = "T")
        {
            DataSet Result = new DataSet();
            DataSet Return = new DataSet();
            DataRow dwTemp = null;
            try
            {
                switch (mtype)
                {
                    case "T":
                        dwTemp = INTEXDB.FSSTAMM.NewRow();
                        dwTemp["FSMANDANT"] = dwInput["FSMANDANT"];
                        dwTemp["FSART"] = dwInput["FSART"];
                        Result = INTEXDBC.QueryFSSTAMM(dwTemp, 2);
                        Return.Merge(Result);
                        dwTemp = INTEXDB.FSGROE.NewRow();
                        dwTemp["FSGMANDANT"] = dwInput["FSMANDANT"];
                        dwTemp["FSGART"] = dwInput["FSART"];
                        Result = INTEXDBC.QueryFSGROE(dwTemp, 3);
                        Return.Merge(Result);
                        dwTemp = INTEXDB.FSFARBEN.NewRow();
                        dwTemp["FSFMANDANT"] = dwInput["FSMANDANT"];
                        dwTemp["FSFART"] = dwInput["FSART"];
                        Result = INTEXDBC.QueryFSFARBEN(dwTemp, 3);
                        Return.Merge(Result);
                        break;
                    case "M":
                        dwTemp = INTEXDB.WASTAMM.NewRow();
                        dwTemp["WAMANDANT"] = dwInput["WAMANDANT"];
                        dwTemp["WAART"] = dwInput["WAART"];
                        Result = INTEXDBC.QueryWASTAMM(dwTemp, 31);
                        Result.Tables[0].TableName = "FSSTAMM";
                        Return.Merge(Result);
                        dwTemp = INTEXDB.WAFARBEN.NewRow();
                        dwTemp["WAFMANDANT"] = dwInput["WAMANDANT"];
                        dwTemp["WAFART"] = dwInput["WAART"];
                        Result = INTEXDBC.QueryWAFARBEN(dwTemp, 51);
                        Result.Tables[0].TableName = "FSFARBEN";
                        Return.Merge(Result);
                        break;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return Return;
        }
        public DataSet Convert2FSSTUELI(DataRow dwData)
        {

            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            string mapID = "FSSTUELI_MAP";
            DataTable tbMapID = new DataTable();

            tbMapID = SetCAD.Tables[mapID];
            DataTable tbIntex = new DataTable();
            tbIntex.TableName = "FSSTUELI";
            DataRow dwIntex = tbIntex.NewRow();
            var query = from dwMap in tbMapID.AsEnumerable()
                        where dwMap["FILE"].ToString().Trim() == "FSSTUELI"
                        select dwMap;
            foreach (DataRow dwTemp in query)
            {
                Convert2Intex.GetCovnertValue(ref dwIntex, dwTemp, dwData);
            }
            tbIntex.Rows.Add(dwIntex);
            setReturn.Merge(tbIntex);
            return setReturn;
        }
        public DataSet Convert2FDECKB(DataRow dwData)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            string mapID = "FDECKB_MAP";
            DataTable tbMapID = new DataTable();
            tbMapID = SetCAD.Tables[mapID];
            DataTable tbIntex = new DataTable();
            tbIntex.TableName = "FDECK";
            DataRow dwIntex = tbIntex.NewRow();
            var query = from dwMap in tbMapID.AsEnumerable()
                        where dwMap["FILE"].ToString().Trim().Trim() == "FDECK"
                        select dwMap;
            foreach (DataRow dwTemp in query)
            {
                Convert2Intex.GetCovnertValue(ref dwIntex, dwTemp, dwData);
            }
            tbIntex.Rows.Add(dwIntex);
            setReturn.Merge(tbIntex);
            return setReturn;
        }
        public DataSet Convert2WDECK(DataRow dwData)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            string mapID = "WDECK_MAP";
            DataTable tbMapID = new DataTable();
            tbMapID = SetCAD.Tables[mapID];
            DataTable tbIntex = new DataTable();
            tbIntex.TableName = "WDECK";
            DataRow dwIntex = tbIntex.NewRow();
            var query = from dwMap in tbMapID.AsEnumerable()
                        where dwMap["FILE"].ToString().Trim().Trim() == "WDECK"
                        select dwMap;
            foreach (DataRow dwTemp in query)
            {
                Convert2Intex.GetCovnertValue(ref dwIntex, dwTemp, dwData);
            }
            tbIntex.Rows.Add(dwIntex);
            setReturn.Merge(tbIntex);
            return setReturn;
        }
        public DataSet GetFSList(DataRow dwInput, int option = 1)
        {
            DataSet Result = new DataSet();
            DataSet Return = new DataSet();
            DataRow dwTemp = null;
            try
            {
                switch (option)
                {
                    case 1:
                        Result = INTEXDBC.QueryFSSTAMM(dwInput, 4);
                        Return.Merge(Result);
                        break;
                    case 2:
                        Result = INTEXDBC.QueryFSSTAMM(dwInput, 6);
                        Return.Merge(Result);
                        break;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return Return;
        }
        public DataSet GetData(DataRow dwInput)
        {
            DataSet Result = new DataSet();
            DataSet Return = new DataSet();
            DataRow dwTemp = null;
            try
            {
                dwTemp = INTEXDB.FSSTAMM.NewRow();
                dwTemp["FSMANDANT"] = dwInput["FSMANDANT"];
                dwTemp["FSART"] = dwInput["FSART"];
                Result = INTEXDBC.QueryFSSTAMM(dwTemp, 2);
                Return.Merge(Result);
                dwTemp = INTEXDB.FSSTUELI.NewRow();
                dwTemp["FSSMANDANT"] = dwInput["FSMANDANT"];
                dwTemp["FSSART"] = dwInput["FSART"];
                Result = INTEXDBC.QueryFSSTUELI(dwTemp, 2);
                Return.Merge(Result);
                dwTemp = INTEXDB.FSFARBEN.NewRow();
                dwTemp["FSFMANDANT"] = dwInput["FSMANDANT"];
                dwTemp["FSFART"] = dwInput["FSART"];
                Result = INTEXDBC.QueryFSFARBEN(dwTemp, 3);
                Return.Merge(Result);
                dwTemp = INTEXDB.FSGROE.NewRow();
                dwTemp["FSGMANDANT"] = dwInput["FSMANDANT"];
                dwTemp["FSGART"] = dwInput["FSART"];
                Result = INTEXDBC.QueryFSGROE(dwTemp, 3);
                Return.Merge(Result);
                dwTemp = INTEXDB.FSSTAMM.NewRow();
                dwTemp["FSMANDANT"] = dwInput["FSMANDANT"];
                dwTemp["FSART"] = dwInput["FSART"];
                dwTemp = INTEXDB.ABPOS.NewRow();
                dwTemp["ABPMANDANT"] = dwInput["FSMANDANT"];
                dwTemp["ABPART"] = dwInput["FSART"];
                Result = INTEXDBC.QueryABPOS(dwTemp, 10);
                Return.Merge(Result);
                //BuildData(dwTemp);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return Return;
        }
        public DataSet GetABData(DataRow dwInput, string mart)
        {
            DataSet Result = new DataSet();
            DataSet Return = new DataSet();
            DataRow dwTemp = null;
            try
            {
                dwTemp = INTEXDB.FSSTAMM.NewRow();
                dwTemp["FSMANDANT"] = dwInput["FSMANDANT"];
                dwTemp["FSART"] = dwInput["FSART"];
                Result = INTEXDBC.QueryFSSTAMM(dwTemp, 2);
                Return.Merge(Result);
                dwTemp = INTEXDB.FSSTUELI.NewRow();
                dwTemp["FSSMANDANT"] = dwInput["FSMANDANT"];
                dwTemp["FSSART"] = dwInput["FSART"];
                dwTemp["FSSMART"] = Left(mart + "                             ", 20);
                Result = INTEXDBC.QueryFSSTUELI(dwTemp, 6);
                Result.Tables[0].TableName = "FSSTUELI_EXT";
                Return.Merge(Result);
                dwTemp = INTEXDB.ABPOS.NewRow();
                dwTemp["ABPMANDANT"] = dwInput["FSMANDANT"];
                dwTemp["ABPART"] = dwInput["FSART"];
                Result = INTEXDBC.QueryABPOS(dwTemp, 10);
                Return.Merge(Result);
                //BuildData(dwTemp);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return Return;

        }

        public DataSet SaveText(DataRow dwInput, string text)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult;
            decimal TXTIPIND = 1;
            //FSSART FSSPOS
            string FSSART = dwInput["FSSART"].ToString().Trim();
            string FSSPOS = dwInput["FSSPOS"].ToString().Trim();
            string TXTISIND = Left(FSSART + "                                             ", 20) + Right("0000" + FSSPOS, 4) + "                                    ";
            try
            {
                DataRow dwData = INTEXDB.TXTIND.NewRow();
                dwData["TXTIMANDANT"] = "00";
                dwData["TXTITXTART"] = "STP";
                dwData["TXTISIND"] = TXTISIND;
                SResult = INTEXDBC.QueryTXTIND(dwData, 2);
                if (SResult.Tables[0].Rows.Count == 0)
                {
                    SResult = INTEXDBC.QueryTXTIND(dwData, 3);
                    if (SResult.Tables.Count > 0)
                    {
                        TXTIPIND = System.Convert.ToDecimal(SResult.Tables[0].Rows[0]["TXTIPIND"].ToString()) + 1;
                        dwData["TXTISPRACHKZ"] = " ";
                        dwData["TXTIPIND"] = TXTIPIND;
                        dwData["TXTITITEL"] = "                              ";
                        dwData["TXTINURERGAENZ"] = " ";
                        dwData["TXTISAPTEXTART"] = "    ";
                        dwData["TXTISTARTENDE"] = " ";
                        dwData["TXTIKZPERS"] = " ";
                        dwData["TXTIAENDBEN"] = "        ";
                        dwData["TXTIAENDDAT"] = 0;
                        dwData["TXTIAENDZL"] = 0;
                        dwData["TXTILPOS"] = 0;
                        dwData["TXTIRICHEDIT"] = " ";
                        dwData["TXTIFILLER"] = "                    ";
                        dwData["TXTINEUBEN"] = UserID;
                        dwData["TXTINEUDAT"] = date;
                        dwData["TXTIUPDT"] = "J";
                        INTEXDBC.SaveTXTIND(dwData);
                    }
                }
                else
                {
                    TXTIPIND = System.Convert.ToDecimal(SResult.Tables[0].Rows[0]["TXTIPIND"].ToString());
                }
                dwData = INTEXDB.TXTKON.NewRow();
                dwData["TXTKSL"] = 26;
                dwData["TXTKFILLER"] = "  ";
                dwData["TXTKMANDANT"] = "00";
                dwData["TXTKPIND"] = TXTIPIND;
                dwData["TXTKPOS"] = 10;
                dwData["TXTKATR"] = 0;
                dwData["TXTKTEXT"] = Left(text + "                                                                                                                                                                  ", 80);
                INTEXDBC.SaveTXTKON(dwData);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        public void InsertTXTKON(DataRow dwVKPOS, int linestart, int lineend, int lineuser, string user, string type = "KVKTTXT")
        {
            DataSet setResult;
            string text;
            string TXTIPIND;
            string field;
            OracleCommand or1 = OracleDB.CreateCommand();
            string SqlInsert = "";
            string INDTYPE = "";
            try
            {
                if (type == "KVKTTXT")
                {
                    TXTIPIND = dwVKPOS["KTXTIPIND"].ToString();
                }
                else
                {
                    TXTIPIND = dwVKPOS["PTXTIPIND"].ToString();
                }
                for (int TXTKPOS = linestart; TXTKPOS < lineend + 2; TXTKPOS++)
                {
                    text = "";

                    string pos = "";
                    if (TXTKPOS == lineend + 1)
                    {
                        TXTKPOS = lineuser;
                        text = user;
                    }
                    else
                    {
                        pos = Right("0" + TXTKPOS.ToString(), 2);
                        string column = Left(type + pos, 9);
                        text = dwVKPOS[column].ToString();
                    }

                    if (text.Trim().Count() == 0)
                    {
                        text = ".";
                    }
                    if (text == user && TXTKPOS == 0)
                    {
                        break;
                    }
                    text = ConvertGB2312To8859P1(text);
                    text = Left(text + "                                                                                ", 80);
                    or1.CommandText = "SELECT COUNT(*)  AS NUMEXIST FROM TXTKON "
                    + " WHERE TXTKMANDANT='" + MANDANT + "' AND TXTKPIND=" + TXTIPIND + " AND TXTKPOS=" + (TXTKPOS * 10).ToString();
                    setResult = Connect2Proxy(or1);
                    if (setResult.Tables.Count > 0)
                    {
                        int NUMEXIST = System.Convert.ToInt32(setResult.Tables[0].Rows[0]["NUMEXIST"].ToString());
                        if (NUMEXIST > 0)
                        {
                            #region UpdateTXTKON
                            or1.CommandText = "UPDATE TXTKON SET TXTKTEXT='" + text + "'"
                           + " WHERE TXTKMANDANT='" + MANDANT + "' AND TXTKPIND=" + TXTIPIND + " AND TXTKPOS=" + (TXTKPOS * 10).ToString(); ;
                            setResult = Connect2Proxy(or1);
                            #endregion
                        }
                        else
                        {
                            #region InsertTXTKON
                            or1.CommandText = "INSERT INTO TXTKON "
                            + " SELECT 20, '  ', '" + MANDANT + "', " + TXTIPIND + ", " + (TXTKPOS * 10).ToString() + ", 0, '" + text + "' "
                            + " FROM DUAL";
                            setResult = Connect2Proxy(or1);
                            #endregion
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void UpdateTXTKON(DataTable tbVKPOS, string userID, string userGroup)
        {
            try
            {
                int linestart = 1;
                int lineend = 1;
                int lineuser = 31;
                foreach (DataRow dwVKPOS in tbVKPOS.Rows)
                {
                    switch (userGroup)
                    {
                        case "EDIT_VK":
                            linestart = 1;
                            lineend = 12;
                            lineuser = 31;
                            InsertTXTKON(dwVKPOS, linestart, lineend, lineuser, userID, "KVKTTXT");
                            return;
                            break;
                        case "EDIT_TE":
                            linestart = 1;
                            lineend = 5;
                            lineuser = 31;
                            break;
                        case "EDIT_TEM":
                            linestart = 6;
                            lineend = 8;
                            lineuser = 32;
                            break;
                        case "EDIT_EK":
                            linestart = 9;
                            lineend = 13;
                            lineuser = 33;
                            break;
                        default:
                            linestart = 1;
                            lineend = 30;
                            lineuser = 0;
                            InsertTXTKON(dwVKPOS, linestart, lineend, lineuser, userID, "KVKTTXT");
                            break;
                    }
                    InsertTXTKON(dwVKPOS, linestart, lineend, lineuser, userID, "PVKTTXT");
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
